package com.example.demo.controller;

import com.example.demo.entity.Licence;
import com.example.demo.entity.Progress;
import com.example.demo.entity.Response;
import com.example.demo.entity.Subject;
import com.example.demo.service.Impl.ProgressImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/progress")
@RequiredArgsConstructor
public class ProgressController {
    private final ProgressImpl progressImpl;
    @PostMapping("/updateByScore")
    public ResponseEntity<Response> updateByScore(@RequestParam String score,@RequestParam Long examId) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateByScore",progressImpl.updateByScore(score,examId)))
                            .message("更新成绩")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新成绩失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @GetMapping("/findAllProgress")
    public ResponseEntity<Response> findAllProgress() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getAllProgress",progressImpl.findAllProgress()))
                        .message("获取所有学习进度信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/findByStudentID/{studentId}")
    public ResponseEntity<Response> findByStudentID(@PathVariable Long studentId) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("findByStudentID",progressImpl.findByStudentID(studentId)))
                        .message("获取所有学习进度信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/findApplicationAndExam/{studentId}")
    public ResponseEntity<Response> findApplicationAndExam(@PathVariable Long studentId) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("findApplicationAndExam",progressImpl.findApplicationAndExam(studentId)))
                        .message("获取所有学习进度信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/addProgress")
    public ResponseEntity<Response> addProgress(@RequestBody Progress progress) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addProgress",progressImpl.addProgress(progress)))
                            .message("添加学习进度")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("添加学习进度失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @DeleteMapping("/deleteProgress")
    public ResponseEntity<Response> deleteProgress(@RequestParam Long progressId) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteProgress",progressImpl.deleteProgress(progressId)))
                            .message("删除学习进度")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除学习进度失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/updateProgress")
    public ResponseEntity<Response> updateProgress(@RequestBody Progress progress) {
        System.out.println(progress);
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateProgress",progressImpl.updateProgress(progress)))
                            .message("更新学习进度")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新学习进度失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
}
